package com.changgou.canal.listener;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.changgou.common.constant.RabbitMQConstant;
import com.changgou.common.dto.Message;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.EnableCanalClient;
import com.xpand.starter.canal.annotation.ListenPoint;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@CanalEventListener
public class SpuDataEventListener {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 自定义监听
     * destination 安装canal的时候指定名字；一般是example
     * schema 数据库
     * table 表
     * eventType 事件类型；新增、修改、删除
     *
     * @param eventType 事件类型；新增、修改、删除
     * @param rowData   行数据
     * @return
     */
    @ListenPoint(destination = "example",schema = "changgou_goods",table = {"tb_spu"},
            eventType = {CanalEntry.EventType.UPDATE, CanalEntry.EventType.DELETE})
    public void onEvent4(CanalEntry.EventType eventType,CanalEntry.RowData rowData) {
        String spuId = getColumeValue(rowData.getAfterColumnsList(), "id");

        switch (eventType) {
            case UPDATE:
                String status = getColumeValue(rowData.getAfterColumnsList(), "status");
                String isDelete = getColumeValue(rowData.getAfterColumnsList(), "is_delete");

                if ("1".equals(isDelete)) {
                    //已经被逻辑删除；也需要删除对应的静态页面
                    //Message message = new Message(spuId, Message.TYPE_GOODS_DELETE);
                    sendMQMsg(spuId, Message.TYPE_GOODS_DELETE);
                } else if ("1".equals(status)) {
                    //如果审核通过
                    //发送MQ消息
                    //Message message = new Message(spuId, Message.TYPE_GOODS_AUDIT);
                    sendMQMsg(spuId, Message.TYPE_GOODS_AUDIT);
                }

                break;
            default:
                //删除
                //Message message = new Message(spuId, Message.TYPE_GOODS_DELETE);
                sendMQMsg(spuId, Message.TYPE_GOODS_DELETE);
        }
    }

    /**
     * 发送消息到RabbitMQ
     *
     * @param content 消息
     * @param type 消息
     */
    private void sendMQMsg(String content, String type) {
        Message message = new Message(content, type);
        rabbitTemplate.convertAndSend(RabbitMQConstant.SPU_TOPIC_EXCHANGE,
                RabbitMQConstant.SPU_TOPIC_QUEUE,message);
    }

    /**
     * 获取修改的那一行某一列的列值
     * @param columnsList 一行数据（有很多列）
     * @param columnName  列名
     * @return 列值
     */
    private String getColumeValue(List<CanalEntry.Column> columnsList, String columnName) {
        if (columnsList!=null&&columnsList.size()>0){
            for (CanalEntry.Column column : columnsList) {
                if (columnName.equals(column.getName())){
                    return column.getValue();
                }
            }
        }
        return null;
    }


}
